package hr.fer.pus.dll_will.tests.sp;

import hr.fer.pus.dll_will.sp.data.ServiceFile;
import hr.fer.pus.dll_will.sp.data.User;
import hr.fer.pus.dll_will.sp.data.XMLMiner;

import java.io.IOException;
import java.util.Set;

import org.junit.Assert;
import org.junit.Test;

/**
 * XML reader jUnit tests.
 *
 */
public class XMLTests {

	/**
	 * Test if users are read correctly. Reading is done from 
	 * {@code test-data/users-test.xml} file.
	 * @throws IOException
	 */
	@Test
	public void readUsersTest() throws IOException {
		Set<User> users = XMLMiner.getUsers("test-data/users-test.xml");
		
		if (users.size() != 3) Assert.fail("Invalid number of users!");
		
		boolean pass = false;
		
		for (User u : users) {
//			  <user>
//			    <id>2</id>
//			    <username>will2</username>
//			    <password>alksjdlkajsdlkasjdlkasjd222</password>
//			  </user>
			if (u.getId() == 2) {
				pass = "will2".equals(u.getUsername())
						&& "alksjdlkajsdlkasjdlkasjd222".equals(u.getPassword());
			}
		}
		
		Assert.assertTrue(pass);
	}
	
	/**
	 * Test if files are read correctly. Reading is done from 
	 * {@code test-data/files-test.xml} file.
	 * @throws IOException
	 */
	@Test
	public void readFilesTest() throws IOException {
		Set<ServiceFile> files = XMLMiner.getFiles("test-data/files-test.xml");
		
		if (files.size() != 3) Assert.fail("Invalid number of files!");
		
		boolean pass = false;
		
		for (ServiceFile f : files) {
//		  <file>
//		    <id>3</id>
//		    <ownerId>2</ownerId>
//		    <filename>file3.txt</filename>
//		    <description>_description_3</description>
//		    <content>_file content_3</content>
//		  </file>
			if (f.getId() == 3) {
				pass = f.getOwnerId() == 2
						&& "file3.txt".equals(f.getFilename())
						&& "_description_3".equals(f.getDescription())
						&& "_file content_3".equals(f.getContent());
			}
		}
		
		Assert.assertTrue(pass);
	}
}
